﻿using MediatR;
using Medical.Api.Read.Application.Command.RecyclingCleaning;
using Medical.Api.Read.Dto.RecyclingCleaning;
using Medical.Domain.Enum;
using Medical.Domain.RecoveryCleaning;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Medical.Api.Read.Application.Handler.RecyclingCleaning
{
    /// <summary>
    /// 获取清洗完成列表信息命令处理
    /// </summary>
    public class GetOffTheStocksCommandHandlter : IRequestHandler<GetOffTheStocksCommand, ApiResult<List<OffTheStocksDtos>>>
    {
        private readonly IBaseRepository<CleaningRegistration> _cleaningRegistrationRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="cleaningRegistrationRepository">清洗登记仓储</param>
        public GetOffTheStocksCommandHandlter(IBaseRepository<CleaningRegistration> cleaningRegistrationRepository)
        {
            _cleaningRegistrationRepository = cleaningRegistrationRepository;
        }

        /// <summary>
        /// 处理获取清洗完成列表信息命令
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>清洗完成列表信息</returns>
        public async Task<ApiResult<List<OffTheStocksDtos>>> Handle(GetOffTheStocksCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 根据清洗状态为已完成查询列表
                var query = _cleaningRegistrationRepository.GetAll()
                    .Where(x => !x.IsDeleted && x.CleaningState == RecoveryStatusEnum.已完成);

                // 如果提供了完成时间作为过滤条件
                if (!string.IsNullOrEmpty(request.FinishTime))
                {
                    // 解析完成时间字符串为日期
                    if (DateTime.TryParse(request.FinishTime, out DateTime finishTime))
                    {
                        // 过滤当天的记录
                        var nextDay = finishTime.AddDays(1);
                        query = query.Where(x => x.FinishTime >= finishTime && x.FinishTime < nextDay);

                    }
                }

                // 按完成时间降序排序
                query = query.OrderByDescending(x => x.FinishTime);

                // 执行查询并映射到DTO
                var result = query.Select(x => new OffTheStocksDtos
                {
                    Id = x.Id,
                    CleaningBatch = x.CleaningBatch,
                    Cleaner = x.Cleaner,
                    FinishTime = x.FinishTime,

                }).ToList();

                return new ApiResult<List<OffTheStocksDtos>>
                {
                    Code = ApiEnums.Success,
                    Msg = "获取清洗完成列表成功",
                    Data = result
                };
            }
            catch (Exception ex)
            {
                return new ApiResult<List<OffTheStocksDtos>>
                {
                    Code = ApiEnums.Error,
                    Msg = $"获取清洗完成列表异常：{ex.Message}",
                    Data = null
                };
            }
        }
    }
}
